package Program.UserUI;

import Program.Information.User;
import Program.Information.UsertypeFee;
import Program.SqlTool.UserDAO;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class FeePaneSon3 extends JPanel {
    private User user;
    private JTable table;
    private JScrollPane jScrollPane;
    private DefaultTableModel dtm;
    private String[] titles = {"类型", "信息"};
    private Object[][] data;
    private UsertypeFee utf;

    public FeePaneSon3() {
        super(null);
        setBounds(0, 0, 900, 600);
        setBackground(Color.WHITE);
        add(getViewTable());
    }

    public FeePaneSon3(User user) {
        super(null);
        this.user = user;
        setBounds(0, 0, 900, 600);
        setBackground(Color.WHITE);
        add(initScrollPane());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(new ImageIcon("Application_images\\userBG2.png").getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
    }

    private JScrollPane initScrollPane() {
        jScrollPane = new JScrollPane();
        jScrollPane.getViewport().setOpaque(false);
        jScrollPane.setOpaque(false);
        jScrollPane.setBounds(200, 150, 500, 230);
        JTable viewTable = getViewTable();
        jScrollPane.setViewportView(viewTable);
        return jScrollPane;
    }

    private JTable getViewTable() {
        data = createArr();
        dtm = new DefaultTableModel(data, titles);
        table = new JTable(dtm) {
            // 重写这个表格的方法：设置不可编辑，但可以选中
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 16));
        table.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        table.setRowHeight(40);
        table.getTableHeader().setReorderingAllowed(false);// 设置列不可拖动
        table.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
        // 设置居中
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(JLabel.CENTER);
        renderer.setOpaque(false);
        table.setDefaultRenderer(Object.class, renderer);
        table.setOpaque(false);    // 设置表格本身透明
        table.getTableHeader().setOpaque(false);    //设置透明
        return table;
    }

    private Object[][] createArr() {
        utf = UserDAO.getUsertypeFee(user.getUsertype());
        Object[][] arr = {
                {"用户类型", utf.getUsertype()},
                {"水费", utf.getWaterFee() + " 元/立方米"},
                {"电费", utf.getElectricFee() + " 元/度"},
                {"天然气费", utf.getGasFee() + " 元/天"},
                {"停车费", utf.getParkFee() + " 元/方"},
        };
        return arr;
    }
}
